$(function () {
    init()
    function init() {
        new Vue({
            el:'#login_form',
            data:{
                action:'/admin_login',
                flag:true,//true:登录模式，false:注册模式
                btnText:'登录',
                tips:'没有账号？',
                account:'',
                name:'',
                password:'',
                password2:''
            },
            methods:{
                changeFlag:function () {
                    if (this.flag){
                        this.flag=false
                        this.btnText='注册'
                        this.tips='已有账号'
                        this.action='/register_user'
                    }else{
                        this.flag=true
                        this.btnText='登录'
                        this.tips='没有账号？'
                        this.action='/login_user'
                    }

                },
                login:function () {
                    if (this.flag){
                        if (this.account===''||this.password===''){
                            alert("请输入账号密码后再登录")
                            return
                        }
                        commonAjax(this.action,{'account':this.account,"password":this.password})
                    }else{
                        if (this.name===''||this.password===''||this.password2===''||this.password!==this.password2){
                            alert("请把注册信息填写完整后再注册")
                            return
                        }
                        commonAjax(this.action,{'name':this.name,"password":this.password})
                    }
                }
            }
        })
    }

    function commonAjax(url,data) {
        // axios.post(url,data).then(function(res){
        //     console.log(res);
        //     if (res.data.code==0){
        //         window.location.href="http://localhost/"+res.data.redirect
        //     }else{
        //         alert("登录失败："+res.msg)
        //     }
        // }).catch(function (error) {
        //     console.log(error);
        // });
        axios({
            method: 'post',
            url: url,
            data: data,
            transformRequest: [
                function (data) {
                    var ret = ''
                    for (var it in data) {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    ret = ret.substring(0, ret.lastIndexOf('&'));
                    return ret
                }
            ],
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(function (value) {
            console.log(value);
                if (value.data.code==0){
                    var address=document.location.hostname+':'+document.location.port
                    window.location.href="http://"+address+value.data.redirect
                }else{
                    alert("登录失败："+res.msg)
                }
        }).catch(function (reason) {alert("登录失败")})
    }
})